import Cfg from './config.js';
import Cry from './crypto.js';
import Msg from './message.js';
import Nav from './navigation.js';

const _post = (url,data,success,hook,fail) =>{
	_http('POST',url,data,success,hook,fail);
}

const _get = (url,data,success,hook,fail) =>{
	_http('GET',url,data,success,hook,fail);
}

const _delete = (url,data,success,hook,fail) =>{
    _http('DELETE',url,data,success,hook,fail);
}

const _put = (url,data,success,hook,fail) =>{
    _http('PUT',url,data,success,hook,fail);
}

const _setToken = (token)=>{
	uni.setStorageSync('_TOKEN_',token);
}

const _getToken = () =>{
	return uni.getStorageSync('_TOKEN_') || '';
}

const _delToken = () =>{
	uni.removeStorageSync('_TOKEN_');
}

const _setAgreement = (agreement)=>{
	uni.setStorageSync('_AGREEMENT_',agreement);
}

const _getAgreement = () =>{
	return uni.getStorageSync('_AGREEMENT_') || '';
}

const _delAgreement = () =>{
	uni.removeStorageSync('_AGREEMENT_');
}

const _setInviterId = (inviterid)=>{
	uni.setStorageSync('_INVITERID_',inviterid);
}

const _getInviterId = () =>{
	return uni.getStorageSync('_INVITERID_') || '';
}

const _delInviterId= () =>{
	uni.removeStorageSync('_INVITERID_');
}

const _setSalesMan= (salesman)=>{
	uni.setStorageSync('_SALESMAN_',salesman);
}

const _getSalesMan = () =>{
	return uni.getStorageSync('_SALESMAN_') || '';
}

const _delSalesMan= () =>{
	uni.removeStorageSync('_SALESMAN_');
}

//网络请求
const _http = (type,url,data,success,hook,fail)=>{
	let that = this;
	let token = 'Bearer ' + _getToken();
	// let token = 'Bearer ';
	//console.log(token);
	data = Object.assign({token:token}, data); 
		//console.log(Cfg.Setting.root)
		// uni.showLoading({
		// 	 title: '加载中',
		// 	 mask:false
		// })
	uni.request({
		url:Cfg.Setting.root+url,
		data:data,		
		dataType:"json",
		method:type,		
		header:{
			'content-type':'application/x-www-form-urlencoded',
			'Authorization':token
		},
		success(res){
			 //console.log(res)	 
			if (res.data.code == 399) {
				Msg.Fail(res.data.msg);
			  uni.navigateBack({});
			  return false;          
			}
			
			if (res.data.code == 630) {			
				uni.showModal({
					title: '提示',
					content: '请先购买跑腿劵',
					success: function (res) {
						if (res.confirm) {
							//console.log('用户点击确定');
							Nav._to({ url: '/pages/personalcenter/buycoupon', type: 0 });
							
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}
					}
				});
			  return false;          
			}
			
			if (res.data.code == 401) {
				uni.showModal({
					title: '提示',
					content: '用户未登录,是否登录',
					success: function (res) {
						if (res.confirm) {
							//console.log('用户点击确定');
							Nav._to({ url: '/pages/public/choose', type: 0 });
							
						} else if (res.cancel) {
							//console.log('用户点击取消');
						}
					}
				});	 
			  return false;          
			}
			
			if (res.data.code == 610) {			
				uni.showModal({
					title: '提示',
					content: res.data.msg,
					showCancel:false,
					success: function (res) {
						if (res.confirm) {
							//console.log('用户点击确定');		
							uni.navigateBack({});
						} else if (res.cancel) {
							//console.log('用户点击取消');
							
						}
					}
				});
			  return false;          
			}
			
			if (res.data.code != 200 && res.data.code != 600) {
				// uni.hideLoading();
				//console.log(23); 
				
				Msg.Fail(res.data.msg);
				return false;			  
			}else{
				success && success(res.data);
			}       			  
		},
		fail(res){
			if(hook){
				fail && fail('网络不给力');
			}else{
				// Msg.Fail(res.errMsg);
				Msg.Fail('网络不给力');
				fail && fail(res);
			}
		},
		complete(res){
			//complete && complete(res);			
		} 
	})	
}

module.exports = {    
	_get,
	_post,
    _delete,
    _put,
	_setToken,
	_getToken,
	_delToken,
	_setAgreement,
	_getAgreement,
	_delAgreement,
	_setInviterId,
	_getInviterId,
	_delInviterId,
	_setSalesMan,
	_getSalesMan,
	_delSalesMan,
}